package cc.togetherit.jcrawler.crawler;

import java.util.HashMap;
import java.util.Map;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

import cc.togetherit.jcrawler.down.DownEngineConfig;
import cc.togetherit.jcrawler.fetch.FetchConfig;
import cc.togetherit.jcrawler.parse.ParseConfig;
import cc.togetherit.jcrawler.proxy.HttpHead;
import cc.togetherit.jcrawler.util.JaxbUtil;


@XmlAccessorType(XmlAccessType.PUBLIC_MEMBER)
@XmlRootElement
public class CrawlerConfig {
    public static final int PoolType_MEM = 1;
    public static final int PoolType_BDB = 2;
    public static final int PoolType_REDIS = 3;



    private String domain;

    private DownEngineConfig downConfig;

    private FetchConfig fetchConfig;

    private ParseConfig parseConfig;


    public CrawlerConfig() {
        downConfig = new DownEngineConfig();
        parseConfig = new ParseConfig();
        fetchConfig = new FetchConfig();
    }


    @XmlElement(required = true, name = "domain")
    public String getDomain() {
        return domain;
    }

    public void setDomain(String domain) {
        this.domain = domain;
    }

    @XmlElement(required = true, name = "downConfig")
    public DownEngineConfig getDownConfig() {
        return downConfig;
    }

    public void setDownConfig(DownEngineConfig downConfig) {
        this.downConfig = downConfig;
    }


    @XmlElement(required = true, name = "fetchConfig")
    public FetchConfig getFetchConfig() {
        return fetchConfig;
    }

    public void setFetchConfig(FetchConfig fetchConfig) {
        this.fetchConfig = fetchConfig;
    }

    @XmlElement(required = true, name = "parseConfig")
    public ParseConfig getParseConfig() {
        return parseConfig;
    }

    public void setParseConfig(ParseConfig parseConfig) {
        this.parseConfig = parseConfig;
    }

    // public int getPoolType() {
    // return PoolType;
    // }
    //
    // public void setPoolType(int poolType) {
    // PoolType = poolType;
    // }

    public void defaultConfig() {
        downConfig.defaultConfig();
        fetchConfig.defaultConfig();
        parseConfig.defaultConfig();
    }


    public static void main(String[] args) {
        CrawlerConfig config = new CrawlerConfig();
        config.setDomain("vimeo.com");
        // config.setPoolType(0);
        // config.getDownConfig().addHttphead(new HttpHead("User-Agent",
        // "Mozilla/5.0 (Windows NT 5.1; rv:14.0) Gecko/20100101 Firefox/14.0"));
        config.getDownConfig().addHttphead(new HttpHead("User-Agent", "Mozilla/5.0 (Windows NT 5.1; rv:14.0) Gecko/20100101 Firefox/14.0"));
        Map<String, Integer> map = new HashMap<String, Integer>();
        map.put("(.*)", 1);
        config.getFetchConfig().setFetchRegex(map);
        config.getFetchConfig().setParseRegex(map);
        config.getParseConfig().setType(0);
        System.out.println(JaxbUtil.convertToXml(config));
    }
}
